<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        const nums = [-4, -1, 0, 3, 10]


        function solution(nums) {
            const length = nums.length
            let i = 0
            let j = length - 1
            let k = length - 1
            let res = new Array(length).fill(0)

            while (i <= j) {
                let left = nums[i] * nums[i]
                let right = nums[j] * nums[j]
                if (left > right) {
                    res[k--] = left
                    i++
                } else {
                    res[k--] = right
                    j--
                }
            }

            return res
        }

        const res = solution(nums)
        console.log(res)
    </script>



</body>

</html>